//
//  AXFMainTabController.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class AXFMainTabController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabBar
        
        let zhTabBar = ZHTabBar()
        //由于系统tabBar是只读属性不能赋值,采用KVC
        zhTabBar.tag = 2
        self.setValue(zhTabBar, forKey: "tabBar")
        
        //监听按钮点击事件
        
        zhTabBar.shoppingCarClosure = { [weak self] in
            print("购物车被点击了")
            
            let vc = AXFShoppingController()
            vc.dataSource = AXHShopingViewModel.shared.shoppingCarArray
            
            let nav = UINavigationController(rootViewController: vc)
            print(AXHShopingViewModel.shared.shoppingCarArray.count)
            nav.navigationBar.barTintColor = #colorLiteral(red: 0.8622179001, green: 0.8622179001, blue: 0.8622179001, alpha: 0.4448844178)
            self?.present(nav, animated: true, completion: nil)
        }

        self .addChildViewControllers()
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(text(n:)), name: NSNotification.Name(rawValue: SmallViewNotification), object: nil)
    }
    
    //获取传过来的 model
    func text(n:Notification){
        let model = n.object as! ZHSellFastModel
        AXHShopingViewModel.shared.shoppingCarArray.append(model)
        
//        print(AXHShopingViewModel.shared.shoppingCarArray.count)
    }
    //添加子控制器
    private func addChildViewControllers(){
        addChildViewControllers(Vc: ZHHomeVC(), title: "首页", imageName: "v2_home", index: 0)
        
        addChildViewControllers(Vc: AXFSuperMarketController(), title: "闪电超市", imageName: "v2_order",index:1)
//        addChildViewControllers(Vc: AXFShoppingController(), title: "购物车", imageName: "shopCart",index:2)
        addChildViewControllers(Vc: AXFMineController(), title: "我的", imageName: "v2_my",index:2)
    }
    private func addChildViewControllers(Vc:UIViewController,title:String,imageName:String,index:Int){
        Vc.title = title
        //设置默认图片和选中图片
        Vc.tabBarItem.tag = index
        //修改了 tabbar 的图片大小
        let normolImage = UIImage.createNewImage(withColor: UIImage(named: imageName), multiple: 0.77)
        Vc.tabBarItem.image = normolImage?
            .withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
         //修改了 tabbar 的选中图片大小
         let selectedImage = UIImage.createNewImage(withColor: UIImage(named: imageName + "_r"), multiple: 0.77)
        Vc.tabBarItem.selectedImage = selectedImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置默认文字和选中文字
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.lightGray], for: .normal)
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.lightGray], for: .selected)

//        //设置tabbar文字图片的位置
        Vc.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 3, 0, -3)
        Vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
        
        //设置导航控制器
        let Nav = AXFMainNavController(rootViewController: Vc)
        //添加到子控制器
        self.addChildViewController(Nav)
    }

    //让tabBar按钮图片点击后"抖一下"
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index = 0
        
        for subView in tabBar.subviews {
            //会同时打印全部tabBar按钮,通过tag值遍历点击的是哪个按钮
            if subView.isKind(of: NSClassFromString("UITabBarButton")!){
                
                if index == item.tag {
                    
                    for v in subView.subviews {
                        
                        if v.isKind(of: NSClassFromString("UIImageView")!) {
                            
                            //这里已经找到了按钮上的图片,可以进行动画了
                            v.shakeToShowView()
                        }
                    }
                }
                index += 1
            }
        }
    }
}


